package com.test.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import com.test.pojo.Red;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @Aurher: QiaoHang
 * @Description:
 * @Data: 2019/9/15 12:59
 * @Modified By:
 */
@Profile(value = {"empdb","deliver"})
@Configuration
@PropertySource(value = {"classpath:/db.properties"})
public class DBconfig implements EmbeddedValueResolverAware {

    @Value("${db.name}")
    private String user;

    @Value("#{10 - 2}")
    private String num;

    //值解析器
    private StringValueResolver valueResouver;
    private String password;

    @Bean
    public Red red(){
        return new Red();
    }

    @Profile("deliver")
    @Bean(name = "deliverDataSource")
    public DataSource deliverDataSource(@Value("${db.driver}") String driver) throws PropertyVetoException {
        System.out.println(user);
        System.out.println(driver);
        System.out.println(password);
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("jdbc:mysql://39.96.44.223:3306/deliver");
        dataSource.setDriverClass(driver);
        return dataSource;
    }


    @Profile("qiganshop")
    @Bean(name = "qiganshopDataSource")
    public DataSource qiganshopDataSource(@Value("${db.driver}") String driver) throws PropertyVetoException {
        System.out.println(user);
        System.out.println(driver);
        System.out.println(password);
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("jdbc:mysql://39.96.44.223:3306/qiganshopDataSource");
        dataSource.setDriverClass(driver);
        return dataSource;
    }


    @Profile("empdb")
    @Bean(name = "empdbDataSource")
    public DataSource empdbDataSource(@Value("${db.driver}") String driver) throws PropertyVetoException {
        System.out.println(user);
        System.out.println(driver);
        System.out.println(password);
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl("jdbc:mysql://39.96.44.223:3306/empdbDataSource");
        dataSource.setDriverClass(driver);
        return dataSource;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        //值解析器
        this.valueResouver=stringValueResolver;
        password = valueResouver.resolveStringValue("${db.password}");
        System.out.println(num);
    }
}
